package com.shanghaionstar.user.service;

import android.content.Context;

import com.onstar.cn.api.UsersService;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.framework.annotation.http.HeadsValue;
import com.onstar.cn.framework.annotation.http.Request;
import com.onstar.cn.framework.mvp.base.core.BaseService;
import com.onstar.cn.framework.network.HttpInjectUtils;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.enumtype.HttpAccept;
import com.onstar.cn.framework.network.enumtype.HttpMethod;
import com.onstar.cn.framework.network.enumtype.Language;
import com.onstar.cn.framework.utils.GsonHelper;
import com.onstar.cn.more.entity.HeadPhoto;
import com.onstar.cn.more.response.UploadImgRes;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.NetConnectUtil;

import rx.Observable;
import rx.functions.Func0;

/**
 * Created by Mengzhou Gu on 2016/8/22.
 */
public class MoreUpLoadImgService extends BaseService<UploadImgRes> {
    private Context context;

    public MoreUpLoadImgService(Context context) {
        this.context = context;
    }

    @Override
    public RequestEntity getRequestEntity() {
        return null;
    }

    @Override
    public UploadImgRes jsonToBean(String json) {
        return GsonHelper.fromJson(json,UploadImgRes.class);
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.PUT)
    public Observable<UploadImgRes> getObserval(final String bitmapBytes, final String extend) {
        return Observable.defer(new Func0<Observable<UploadImgRes>>() {
            @Override
            public Observable<UploadImgRes> call() {
                try {
                    UsersService service = new UsersService(Constants.CURRENT_SERVER_URL);
                    RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(context);
                    requestEntity = service.uploadHeadPhoto(requestEntity);
                    HeadPhoto headPhoto = new HeadPhoto();
                    String idpId = ApplicationUtil.getInstance().getIdpID();
                    headPhoto.setIdpID(idpId);
                    headPhoto.setUserFace(bitmapBytes);
                    headPhoto.setExtension(extend);
                    requestEntity.setBody(GsonHelper.toJson(headPhoto));
                    requestEntity.setTimeout(2 * 60 * 1000);
                    requestEntity = HttpInjectUtils.injectHttp(MoreUpLoadImgService.this.getClass(), "getObserval", requestEntity, String.class, String.class);
                    UploadImgRes uploadImgRes = GsonHelper.fromJson(BusinessUtil.getResponse(getDatas(requestEntity), context),UploadImgRes.class);
                    return Observable.just(uploadImgRes);
                } catch (OnStarException e) {
                    return Observable.error(e);
                }

            }
        });
    }
}
